package chat.client.view;

import chat.client.controller.UserListCellRenderer;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.util.Random;

/**
 * 客户端窗体样式类
 *
 * #1 定义并创建客户端的窗体样式
 *
 * @author 谈笑、
 * @dateTime 2020/10/21 15:41
 */
public class ClientView {

	// 主窗体
	private JFrame jFrame = new JFrame("TCP客户端");

	// 提示标签
	private JLabel[] jLabels = {
			new JLabel("消息记录"), new JLabel("输入框"),
			new JLabel("在线用户"), new JLabel(""),
			new JLabel("配置服务"), new JLabel("服务器IP地址："),
			new JLabel("服务器端口号："), new JLabel("已连接时长：未连接服务"),
			new JLabel("通信模式"), new JLabel("操作功能"),
			new JLabel("客户端名称：")
	};
	// 组件容器
	private JPanel[] jPanels = {
			new JPanel(), new JPanel(), new JPanel(),
			new JPanel(), new JPanel(), new JPanel(),
			new JPanel(), new JPanel(), new JPanel()
	};

	// 聊天记录框、报文发送框
	private JTextArea[] jTextAreas = {
			new JTextArea(), new JTextArea()
	};

	// IP、Port输入框
	public JTextField[] jTextFields = {
			new JTextField(), new JTextField(),
			new JTextField()
	};

	// 消息发送框操作按钮
	private JButton[] jButtons = {
			new JButton("发送"), new JButton("复制"),
			new JButton("清空"), new JButton("连接服务"),
			new JButton("断开连接"), new JButton("退出程序")
	};

	// 列表模型
	private DefaultListModel<String> defaultListModel;

	// 在线用户列表框
	private JList<String> jList;

	// 通信模式单选框
	public JRadioButton[] jRadioButtons = {
			new JRadioButton("单向通信"),
			new JRadioButton("多向通信", true)
	};

	// 字体
	public Font[] fonts = {
			new Font("微软雅黑", Font.PLAIN, 15),
			new Font("微软雅黑", Font.PLAIN, 16),
			new Font("微软雅黑", Font.PLAIN, 17)
	};

	// 颜色
	private Color[] colors = {
			// 按钮背景、边框
			new Color(252, 252, 252), new Color(165, 165, 165),
			// TCP、UDP模式色
			// 连接状态色
			new Color(210, 104, 104), new Color(120, 212, 105), new Color(212, 196, 106)
	};

	/**
	 * 消息记录组件容器
	 */
	public JPanel getJPanel_0() {
		this.jTextAreas[0].setFont(this.fonts[1]);
		this.jTextAreas[0].setLineWrap(true);
		this.jTextAreas[0].setEditable(false);
		this.jTextAreas[0].setText("");

		// 滚动条
		JScrollPane jScrollPane = new JScrollPane();
		jScrollPane.setBorder(null);
		jScrollPane.setViewportView(this.jTextAreas[0]);
		jScrollPane.setBounds(1, 1, 598, 338);

		// 组件容器
		this.jPanels[0].setBounds(10, 30, 600, 340);
		this.jPanels[0].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jPanels[0].setLayout(null);
		this.jPanels[0].add(jScrollPane);
		return this.jPanels[0];
	}

	/**
	 * 消息输入组件容器
	 */
	public JPanel getJPanel_1() {
		this.jTextAreas[1].setFont(this.fonts[1]);
		this.jTextAreas[1].setLineWrap(true);
		this.jTextAreas[1].setText("");

		// 滚动条
		JScrollPane jScrollPane = new JScrollPane();
		jScrollPane.setBorder(null);
		jScrollPane.setViewportView(this.jTextAreas[1]);
		jScrollPane.setBounds(1, 1, 498, 106);

		// 组件容器
		this.jPanels[1].setBounds(10, 407, 500, 108);
		this.jPanels[1].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jPanels[1].setLayout(null);
		this.jPanels[1].add(jScrollPane);
		return this.jPanels[1];
	}

	/**
	 * 信息息发送组件容器
	 */
	public JPanel getJPanel_2() {
		// 发送按钮
		this.jButtons[0].setBounds(0, 22, 95, 33);
		this.jButtons[0].setFont(this.fonts[0]);
		this.jButtons[0].setBackground(this.colors[0]);
		this.jButtons[0].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jButtons[0].setFocusPainted(false);
		this.jButtons[0].setEnabled(false);

		// 复制按钮
		this.jButtons[1].setBounds(0, 60, 95, 33);
		this.jButtons[1].setFont(this.fonts[0]);
		this.jButtons[1].setBackground(this.colors[0]);
		this.jButtons[1].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jButtons[1].setFocusPainted(false);

		// 清空按钮
		this.jButtons[2].setBounds(0, 97, 95, 33);
		this.jButtons[2].setFont(this.fonts[0]);
		this.jButtons[2].setBackground(this.colors[0]);
		this.jButtons[2].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jButtons[2].setFocusPainted(false);

		// 组件容器
		this.jPanels[2].setBounds(515, 385, 95, 130);
		this.jPanels[2].setLayout(null);
		this.jPanels[2].add(this.jButtons[0]);
		this.jPanels[2].add(this.jButtons[1]);
		this.jPanels[2].add(this.jButtons[2]);
		return this.jPanels[2];
	}

	/**
	 * 在线用户组件容器
	 */
	public JPanel getJPanel_3() {
		// 列表模型
		this.defaultListModel = new DefaultListModel<>();
		//加入头像地址
		// 在线用户列表框
		this.jList = new JList<>(this.defaultListModel);

		Random random = new Random();
		int randomNumber = random.nextInt(10) + 1;
		this.jList.setCellRenderer(new UserListCellRenderer(new ImageIcon("img/"+randomNumber+".png"),30));
		// 禁止多选
		this.jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.jList.setFont(this.fonts[0]);

		// 滚动条
		JScrollPane jScrollPane = new JScrollPane(this.jList);
		jScrollPane.setBounds(1, 1, 198, 483);
		jScrollPane.setViewportView(this.jList);
		jScrollPane.setBorder(null);

		// 组件容器
		this.jPanels[3].setBounds(620, 30, 200, 485);
		this.jPanels[3].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jPanels[3].setLayout(null);
		this.jPanels[3].add(jScrollPane);
		return this.jPanels[3];
	}

	/**
	 * 客户端状态指示灯组件容器
	 */
	public JPanel getJPanel_4() {
		// 指示灯
		this.jLabels[3].setBounds(10, 8, 220, 14);
		this.jLabels[3].setOpaque(true);
		this.jLabels[3].setBackground(this.colors[2]);

		// 组件容器
		this.jPanels[4].setBounds(830, 30, 240, 30);
		this.jPanels[4].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jPanels[4].setLayout(null);
		this.jPanels[4].add(this.jLabels[3]);
		return this.jPanels[4];
	}

	/**
	 * 配置服务端组件容器
	 */
	public JPanel getJPanel_5() {
		// IP地址
		this.jLabels[5].setBounds(5, 10, 105, 20);
		this.jLabels[5].setFont(this.fonts[0]);

		// 端口号
		this.jLabels[6].setBounds(5, 45, 105, 20);
		this.jLabels[6].setFont(this.fonts[0]);

		// 客户端名称
		this.jLabels[10].setBounds(5, 77, 200, 20);
		this.jLabels[10].setFont(this.fonts[0]);
		// 连接时长
		this.jLabels[7].setBounds(5, 109, 200, 20);
		this.jLabels[7].setFont(this.fonts[0]);

		// IP地址输入框
		this.jTextFields[0].setBounds(105, 8, 130,25);
		this.jTextFields[0].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jTextFields[0].setFont(this.fonts[0]);
		// this.jTextFields[0].setText("127.0.0.1");
		this.jTextFields[0].setText("127.0.0.1");

		// 端口输入框
		this.jTextFields[1].setBounds(105, 43, 130,25);
		this.jTextFields[1].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jTextFields[1].setFont(this.fonts[0]);
		this.jTextFields[1].setText("8000");

		// 客户端名称输入框
		this.jTextFields[2].setBounds(105, 75, 130,25);
		this.jTextFields[2].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jTextFields[2].setFont(this.fonts[0]);
		this.jTextFields[2].setText("客户端" + new Random().nextInt(500));

		// 组件容器
		this.jPanels[5].setBounds(830, 95, 240, 135);
		this.jPanels[5].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jPanels[5].setLayout(null);
		this.jPanels[5].add(this.jLabels[5]);
		this.jPanels[5].add(this.jLabels[6]);
		this.jPanels[5].add(this.jLabels[10]);
		this.jPanels[5].add(this.jLabels[7]);
		this.jPanels[5].add(this.jTextFields[0]);
		this.jPanels[5].add(this.jTextFields[1]);
		this.jPanels[5].add(this.jTextFields[2]);
		return this.jPanels[5];
	}

	/**
	 * 通信模式组件容器
	 */
	public JPanel getJPanel_6() {
		// 单方通信单选框
		this.jRadioButtons[0].setBounds(25, 10, 100, 20);
		this.jRadioButtons[0].setFont(this.fonts[0]);
		this.jRadioButtons[0].setFocusPainted(false);

		// 多方通信单选框
		this.jRadioButtons[1].setBounds(130, 10, 100, 20);
		this.jRadioButtons[1].setFont(this.fonts[0]);
		this.jRadioButtons[1].setFocusPainted(false);

		// 单选按钮组
		ButtonGroup buttonGroup = new ButtonGroup();
		buttonGroup.add(this.jRadioButtons[0]);
		buttonGroup.add(this.jRadioButtons[1]);

		// 组件容器
		this.jPanels[6].setBounds(830, 265, 240, 40);
		this.jPanels[6].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jPanels[6].setLayout(null);
		this.jPanels[6].add(this.jRadioButtons[0]);
		this.jPanels[6].add(this.jRadioButtons[1]);
		return this.jPanels[6];
	}

	/**
	 * 功能操作组件容器
	 */
	public JPanel getJPanel_7() {
		// 连接按钮
		this.jButtons[3].setBounds(47, 20, 145, 37);
		this.jButtons[3].setFont(this.fonts[0]);
		this.jButtons[3].setBackground(this.colors[0]);
		this.jButtons[3].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jButtons[3].setFocusPainted(false);

		// 断开连接按钮
		this.jButtons[4].setBounds(47, 67, 145, 37);
		this.jButtons[4].setFont(this.fonts[0]);
		this.jButtons[4].setBackground(this.colors[0]);
		this.jButtons[4].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jButtons[4].setFocusPainted(false);
		this.jButtons[4].setEnabled(false);

		// 退出程序按钮
		this.jButtons[5].setBounds(47, 114, 145, 37);
		this.jButtons[5].setFont(this.fonts[0]);
		this.jButtons[5].setBackground(this.colors[0]);
		this.jButtons[5].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jButtons[5].setFocusPainted(false);

		// 组件容器
		this.jPanels[7].setBounds(830, 340, 240, 175);
		this.jPanels[7].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jPanels[7].setLayout(null);
		this.jPanels[7].add(this.jButtons[3]);
		this.jPanels[7].add(this.jButtons[4]);
		this.jPanels[7].add(this.jButtons[5]);
		return this.jPanels[7];
	}


	// 显示窗体
	public void display() {
		// 设置提示框内的字体样式
		UIManager.put("OptionPane.messageFont", new FontUIResource(this.fonts[1]));
		UIManager.put("OptionPane.buttonFont", new FontUIResource(this.fonts[0]));

		// 消息记录
		this.jLabels[0].setBounds(10, 5, 100, 20);
		this.jLabels[0].setFont(this.fonts[0]);

		// 输入框
		this.jLabels[1].setBounds(10, 382, 100, 20);
		this.jLabels[1].setFont(this.fonts[0]);

		// 在线用户
		this.jLabels[2].setBounds(620, 5, 100, 20);
		this.jLabels[2].setFont(this.fonts[0]);

		// 配置服务
		this.jLabels[4].setBounds(830, 70, 100, 20);
		this.jLabels[4].setFont(this.fonts[0]);

		// 通信模式
		this.jLabels[8].setBounds(830, 240, 100, 20);
		this.jLabels[8].setFont(this.fonts[0]);

		// 操作功能
		this.jLabels[9].setBounds(830, 315, 100, 20);
		this.jLabels[9].setFont(this.fonts[0]);

		this.jFrame.setSize(1085, 560);
		this.jFrame.setLayout(null);
		this.jFrame.setLocationRelativeTo(null);
		this.jFrame.setResizable(false);

		// 添加外部提示标签
		this.jFrame.add(this.jLabels[0]);
		this.jFrame.add(this.jLabels[1]);
		this.jFrame.add(this.jLabels[2]);
		this.jFrame.add(this.jLabels[4]);
		this.jFrame.add(this.jLabels[8]);
		this.jFrame.add(this.jLabels[9]);

		// 添加组件容器
		this.jFrame.add(this.getJPanel_0());
		this.jFrame.add(this.getJPanel_1());
		this.jFrame.add(this.getJPanel_2());
		this.jFrame.add(this.getJPanel_3());
		this.jFrame.add(this.getJPanel_4());
		this.jFrame.add(this.getJPanel_5());
		this.jFrame.add(this.getJPanel_6());
		this.jFrame.add(this.getJPanel_7());
		this.jFrame.setVisible(true);
	}

	public JFrame getjFrame() {
		return jFrame;
	}

	public JLabel[] getjLabels() {
		return jLabels;
	}

	public JTextArea[] getjTextAreas() {
		return jTextAreas;
	}

	public JTextField[] getjTextFields() {
		return jTextFields;
	}

	public JButton[] getjButtons() {
		return jButtons;
	}

	public DefaultListModel<String> getDefaultListModel() {
		return defaultListModel;
	}

	public JList<String> getjList() {
		return jList;
	}

	public JRadioButton[] getjRadioButtons() {
		return jRadioButtons;
	}

	public Color[] getColors() {
		return colors;
	}

}